package com.corner.system.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Slf4j
public class WebSocketServerHandler extends TextWebSocketHandler {

    /**
     * 处理连接后的逻辑
     * @param session:WebSocketSession域对象,有获取通信信息,发送消息,打开关闭通道等等功能
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info("Server: WebSocket connection established");
    }

    /**
     * 处理客户端发送的消息
     * @param session:WebSocketSession域对象,有获取通信信息,发送消息,打开关闭通道等等功能
     * @param message:客户端发送的文本消息
     */
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.info("Server: Received message: " + message.getPayload());
        session.sendMessage(new TextMessage("Server: Hello, client!"));
    }
}